var server = require('../server')
var db = require('../../db/connect')
var jsonres = require('../../libs/jsonres')
var request = require('request')
var config = require('../../config')
var urlencode = require('urlencode2');

var isJson = function(str) {
    try {
        JSON.parse(this.toString())
        return true;
    } catch(e) {
        return false;
    }
}
String.prototype.isJson = isJson;

server.get('/api/toprank',function(req,res) {
    var mobile = req.query.mobile;
    var platform = req.query.platform;
    var uid = '';
    db.find('accounts',{platform: platform, user: mobile},function(err,result){
        if (result.length > 0) {
            uid = result[0].userid;
           // if (uid && uid.length > 0) {
            if (1==1){
                request({
                    url: config.dispatcher_host + `/card?playerid=${mobile}&platform=${urlencode(platform)}`,
                    json: true
                },function(err,httpResponse,body) {
                    if (!err) {
                        res.send(jsonres(200,'success',body))
                    } else {
                        res.send(jsonres(-1,'not found any match data',[]))
                    }
                })
            } else {
                return res.send(jsonres(-1,'not found any userid2',null))
            }
        } else {
            return res.send(jsonres(-1,'not found any userid1',null))
        }
    })
})
